package bhbt.controller.member.kuang;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import bhbt.controller.core.BaseController;
import bhbt.model.Member;
import bhbt.model.ParamWage;
import bhbt.service.DailiService;
import bhbt.service.MemberService;
import bhbt.service.ParamWageService;
import bhbt.service.log.LogKuangService;
import bhbt.utils.ContextUtil;
import bhbt.utils.CryptUtil;
import bhbt.utils.MapUtil;





@Controller
 
public class KuangBuyController extends BaseController {

	@Resource
	private MemberService memberService;
	@Resource
	private LogKuangService logKuangService;
	@Resource
	private DailiService dailiService;
	
	@Resource
	private ParamWageService paramWageService;

	@RequestMapping("member/kuang/kuangBuyShow.shtml")
	public String show(HttpServletRequest request) throws Exception{
		
		//会员信息
		Member member = ContextUtil.getMember();
		member= memberService.getByKey(member.getId());
		request.setAttribute("member",member);
		
		ParamWage paramWage1003=paramWageService.getByKey(1003);
		request.setAttribute("paramWage1003",paramWage1003.getValue());
		
		ParamWage paramWage1002=paramWageService.getByKey(1002);
		request.setAttribute("paramWage1002",paramWage1002.getValue());
		 
		
		return "member/kuang/kuangBuy";
	}
 
	@RequestMapping("member/kuang/kuangBuy.shtml")
	public String ti(HttpServletRequest request, ModelMap model) throws Exception{
		
		Boolean result=true;
		//会员信息
		Member member = ContextUtil.getMember();
		member= memberService.getByKey(member.getId());
		 
		ParamWage paramWage1002=paramWageService.getByKey(1002);
		ParamWage paramWage1003=paramWageService.getByKey(1003);
		ParamWage paramWage1004=paramWageService.getByKey(1004);
		
		
		//今日推出矿点数
		ParamWage paramWage800 = this.paramWageService.getByKey(800);
		//今日可买矿点数
		ParamWage paramWage801 = this.paramWageService.getByKey(801);
		
		
		
		Map formMap = getParameterMap(request);
		String twoPassword = MapUtil.getString(formMap, "twoPassword");
		String pwd = CryptUtil.getInstance().encrypt2String(twoPassword,CryptUtil.keyStr);
   	 
    	if(!member.getTwoPassword().equals(pwd)){
    		setMessage(model,"二级密码不正确。");
    		result=false;
    	}else{
    		int num = MapUtil.getIntValue(formMap, "num");
    		int wageType = MapUtil.getIntValue(formMap, "wageType");
    		if(num<=0){
    			setMessage(model,"请输入购买数。");
        		result=false;
    		}else{
    			if(paramWage801.getValue()<num){
    				setMessage(model,"今日推出的矿点已经全部被抢完，请明天再来。");
            		result=false;
    			}
    			
    			//买矿费用
    			Double fei = num*paramWage1003.getValue();
    			
    			//现金币
    			if(wageType==10){
    				if(member.getMoney()<fei){
    					setMessage(model,"现金币不足。");
    	        		result=false;
    				}
    			}
    			else{
    				setMessage(model,"请选择使用什么币种购买。");
	        		result=false;
    			}
    			
				
    			//判断是否还有资格拥有矿点
    			if(member.getKuang()>=paramWage1004.getValue()||member.getBuyKuang()>=paramWage1004.getValue()){
    				double xianKuangNum = paramWage1004.getValue();
    				Integer xianKuangNumInt =(int)xianKuangNum;
    				setMessage(model,"您已经购买或者拥有矿点超过："+xianKuangNumInt);
            		result=false;
    			}
    			
				
				
				if(result){
					 
    				logKuangService.saveBuyKuang(num,wageType);
    				return "redirect:logKuangNoActiveList.shtml";
				}
				
    		}
    		 
    	}
		
    	if(!result){
    		
    		request.setAttribute("member",member);
    		request.setAttribute("paramWage1002",paramWage1002.getValue());
    		request.setAttribute("paramWage1003",paramWage1003.getValue());
    		request.setAttribute("paramWage1004",paramWage1004.getValue());
    		return "member/kuang/kuangBuy";
    	} 
		
		return "redirect:logKuangNoActiveList.shtml";
	}
	
	 
	
}
